﻿using Engine;
using Game;
using Mekiasm.AnimateModel;
using Mekiasm.Common;

namespace Mekiasm
{
    public class WindPowerGenerator : AnimateModelItem
    {
        public static int Index;
        public WindPowerGenerator() : base(new ModelItem[] {
            new ModelItem("d6", "ports"), new ModelItem("s5", "ports"), new ModelItem("s4", "mekasuit_armor_exoskeleton"),
            new ModelItem("s3", "mekasuit_armor_exoskeleton"), new ModelItem("s2", "mekasuit_armor_exoskeleton"),
            new ModelItem("s1", "mekasuit_armor_exoskeleton"), new ModelItem("d1", "ports"), new ModelItem("d2", "ports"),
            new ModelItem("d3", "mekasuit_armor_exoskeleton"), new ModelItem("d4", "mekasuit_armor_exoskeleton"),
            new ModelItem("d5", "mekasuit_armor_exoskeleton") }, new string[] { "s1", "s2", "s3", "s4", "s5" }, 60, "windgenerator", "风力发电机")
        {
            
            IconScale = 0.5f;
        }
        public override MekBlock.DeviceType GetDeviceType()
        {
            return MekBlock.DeviceType.Electric;
        }
        public override bool OnInteract(SubsystemBlockBehavior blockBehavior, TerrainRaycastResult raycastResult, ComponentMiner componentMiner)
        {
            ComponentWindGenerator mcomponent = blockBehavior.Project.FindSubsystem<SubsystemBlockEntities>().GetBlockEntity(raycastResult.CellFace.X, raycastResult.CellFace.Y, raycastResult.CellFace.Z).Entity.FindComponent<ComponentWindGenerator>(true);
            componentMiner.ComponentPlayer.ComponentGui.ModalPanelWidget = new BatBoxWidget(mcomponent, componentMiner, DisplayName);
            return true;
        }
        public override void OnBlockAdded(SubsystemBlockBehavior blockBehavior, int value, int oldValue, int x, int y, int z)
        {
            base.OnBlockAdded(blockBehavior, value, oldValue, x, y, z);
            ILibrary.AddBaseEnergyEntity(DisplayName, x, y, z, null,
               ILibrary.GetRotetedConfig(value, new MachineFaceConfig[] {
                    new MachineFaceConfig(),
                    new MachineFaceConfig(),
                    new MachineFaceConfig(MekBlock.MachineMode.OUT,MekBlock.DeviceType.Electric),
                    new MachineFaceConfig(),
                    new MachineFaceConfig(),
                    new MachineFaceConfig()
               }), new MachineAmountLimitConfig()
               {
                   MaxPower = 10240,
                   InputPowerLimit = 32,
                   OutputPowerLimit = 32,
               });
            ElectricSystem.Add(new ElectricElement.WindGeneratorElement(x, y, z));
            var frame = blockBehavior.Project.FindSubsystem<SubsystemBlockEntities>().GetBlockEntity(x, y, z).Entity.FindComponent<ComponentFrame>(true);
            frame.Position = new Vector3(x, y, z);
            var m = frame.Entity.FindComponent<ComponentWindGenerate>();
            m.Direction = ILibrary.GetDirection(value);
            m.Rotate = null;

        }
        public override void OnBlockGenerated(int value, int x, int y, int z, bool isLoaded)
        {
            ElectricSystem.Add(new ElectricElement.WindGeneratorElement(x, y, z));
            var frame = ILibrary.Project.FindSubsystem<SubsystemBlockEntities>().GetBlockEntity(x, y, z).Entity.FindComponent<ComponentFrame>(true);
            var m = frame.Entity.FindComponent<ComponentWindGenerate>();
            m.Direction = ILibrary.GetDirection(value);
            m.Rotate = null;
        }
        public override void OnBlockModified(int value, int oldValue, int x, int y, int z)
        {
            var frame = ILibrary.Project.FindSubsystem<SubsystemBlockEntities>().GetBlockEntity(x, y, z).Entity.FindComponent<ComponentFrame>(true);
            var m = frame.Entity.FindComponent<ComponentWindGenerate>();
            m.Direction = ILibrary.GetDirection(value);
            m.Rotate = null;
        }
        public override void OnBlockRemoved(SubsystemBlockBehavior blockBehavior, int value, int newValue, int x, int y, int z)
        {
            ElectricSystem.Remove(new Point3(x, y, z));
            ILibrary.RemoveBlockEntity(x, y, z);
        }

        public override string GetDescription(int value)
        {
            return "廉价，实用，高效的发电设备。";
        }
    }
}
